/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.dataaccess.dao.conference.dao;

import com.nus.dataaccess.dao.base.dao.BaseDao;
import com.nus.dataaccess.dao.conference.ConferenceAuthor;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author robin.foe
 */
public class ConferenceAuthorDao extends BaseDao<ConferenceAuthor>{
    
    private static ConferenceAuthorDao instance;

    public static ConferenceAuthorDao getInstance() {
        if (null == instance) {
            instance = new ConferenceAuthorDao();
        }
        return instance;
    }
    @Override
    protected Class getReferenceClass() {return ConferenceAuthor.class;}
    
    public int getTotalCount(Map<String, Object> filters){
        Criteria crit = super.createCriteria();
        crit.setProjection(Projections.rowCount());
        
        if(filters!=null && !filters.isEmpty()){
            for(String key : filters.keySet() ){
                crit.add(Restrictions.eq(key, filters.get(key)));
            }
        }
        
        return ((Long)crit.list().get(0)).intValue();
    }
    
    public List<ConferenceAuthor> paginate(int first, int pageSize, String sortField, boolean ascending, Map<String, Object> filters){
        Criteria crit = super.createCriteria();
        if(filters!=null && !filters.isEmpty()){
            for(String key : filters.keySet() ){
                crit.add(Restrictions.eq(key, filters.get(key)));
            }
        }
        
        crit.setFirstResult(first);
        crit.setMaxResults(pageSize);
        return crit.list();
    }
    
    public List<ConferenceAuthor> paginateByAuthorId(Long authorId, int first, int pageSize, String sortField, boolean ascending, Map<String, Object> filters){
        
        Criteria crit = super.createCriteria();
        crit.createAlias("authors", "a");
        crit.add(Restrictions.eq("a.userAccountId", authorId));
        
        if(filters!=null && !filters.isEmpty()){
            for(String key : filters.keySet() ){
                System.out.println("key "+key);
                crit.add(Restrictions.eq(key, filters.get(key)));
            }
        }
        
        crit.setFirstResult(first);
        crit.setMaxResults(pageSize);
        return crit.list();
    }
    
    public int getTotalCountByAuthorId(Long authorId, Map<String, Object> filters){
        Criteria crit = super.createCriteria();
        crit.setProjection(Projections.rowCount());
        crit.createAlias("authors", "a");
        crit.add(Restrictions.eq("a.userAccountId", authorId));
        
        if(filters!=null && !filters.isEmpty()){
            for(String key : filters.keySet() ){
                crit.add(Restrictions.eq(key, filters.get(key)));
            }
        }
        
        return ((Long)crit.list().get(0)).intValue();
    }
    
    public List<ConferenceAuthor> getByConferenceId(Long conferenceId){
        Criteria crit = super.createCriteria();
        crit.add(Restrictions.eq("conferenceId", conferenceId));
        return crit.list();
    }
    
}
